<?php

namespace app\admin\controller;

use app\common\controller\Base;
use think\Request;
class Ccolumn extends Base
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $data = db('ccolumn')->select();
        $count = db('ccolumn')->count();
        $data = editTime($data);
        $this->assign('data',$data);
        $this->assign('count',$count);
        return view();
    }

    /**
     * 查询栏目
     */
    public function column()
    {
        $data = db('ccolumn')->field('id,title')->select();
        return json($data);
    }

    /**
     * 保存新建的资源
     */
    public function create()
    {
        return view();
    }

    /**
     * 显示编辑资源表单页.
     */
    public function edit()
    {
        $id = input('get.id');
        $data = db('ccolumn')->where('id',$id)->find();
        $this->assign('data',$data);
        return view();
    }

    /**
     * 显示指定的资源
     */
    public function save()
    {
        if(Request::instance()->isPost()) {
            $data['id'] = input('post.id/d','');
            $data['title'] = input('post.title/s','','/([\x{4e00}-\x{9fa5}]+)/u');
            $data['title'] || jump_page('','栏目名称必须为汉字!',2);
            db('ccolumn')->where('id','NOT IN',$data['id'])->where(['title'=>$data['title']])->find() && jump_page('','该商品栏目已存在!',2);
            $data['sec'] = input('post.sec/s','','/([\x{4e00}-\x{9fa5}]+)/u');
            $data['sec'] || jump_page('','商品栏目描述必须为汉字!',2);
            if($data['id']){
                $column = db('ccolumn')->where('id', $data['id'])->update($data);
                return $column !== false ? _layer_msg('更新成功!','',1) : jump_page('','更新失败!',2);
            }
            $data['addTime'] = time();
            $column = db('ccolumn')->insert($data);
            return $column ? _layer_msg('已添加!','',1) : _layer_msg('添加失败!','admin/Ccolumn/index');
        }
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        $ids = input('post.id','');
        if(!$ids) return json(['status'=>0,'msg'=>'请选择要删除的角色!']);
        return db('ccolumn')->delete($ids) ? json(['status'=>1,'msg'=>'已删除!']) : json(['status'=>0,'msg'=>'删除失败!']);
    }
}
